<?php
require_once('modules/interfaces/iconnection.class.php');
require_once('modules/persistence/connection.class.php');
require_once('modules/exceptions/connectionexception.class.php');
require_once('modules/persistence/mysql/mysqltransaction.class.php');

/**
 * Mysql Database connection management.
 * @author delormef
 * @version 1.0 - 12/12/2009
 */
class MysqlConnection extends Connection implements IConnection {
	/**
	 * Instance of the MySQL connection manager.
	 */
	private static $_instance=null;
	/**
	 * connection paramater
	 * var array
	 */
	private $conn;
	/**
	 * Mysql database connection handle.
	 * @var Object.
	 */
	private $connection;
	
	/**
	 * Default constructeur.
	 */
	public function __construct(){
		parent::__construct();
	}
	
	/**
	 * (non-PHPdoc)
	 * @see modules/IConnection#open()
	 */
	public function open($conn){
		$this->conn=$conn;
		if(!$this->connection = mysql_connect($conn['servername'],$conn['username'],$conn['password'])){
			throw new ConnectionException("Can't connect to '".$conn['servername']."' server.");
		}
		if($this->connection && !mysql_select_db($conn['database'])){
			throw new ConnectionException("Can't connect to '".$conn['database']."' database.");
		}
	}
	/**
	 * (non-PHPdoc)
	 * @see modules/IConnection#begin()
	 */
	public function getTransaction(){
		return new Transaction();
	}
	/**
	 * (non-PHPdoc)
	 * @see modules/IConnection#execute()
	 */
	public function execute($sql){
		__debug(__FILE__,__LINE__,"MySQL::execute() sql=[".$sql."]");
		return mysql_query($sql);		
	}
	/**
	 * execute a Select request and return retrieved data to an array.
	 * @param unknown_type $sql
	 * @return unknown_type
	 */
	public function executeToList($sql){
		__debug(__FILE__,__LINE__,"MySQL::executeToList() sql=[".$sql."]");
		$list=array();
		$rs = mysql_query($sql,$this->connection) or die("ERROR : can't retrieve data on this connection ".print_r($this->connection,true));
		__debug(__FILE__,__LINE__,"rs=".print_r($rs,true));
		if(mysql_num_rows($rs)>0){
			$ret=array();
			$ret=mysql_fetch_array($rs,MYSQL_ASSOC);
			__debug(__FILE__,__LINE__,"fetched : ".print_r($ret,true));
		}else{
			$ret=array();
		}
		return $ret;		
	}
	
	/**
	 * (non-PHPdoc)
	 * @see modules/IConnection#fetch()
	 */
	public function fetch($rs){
		__debug(__FILE__,__LINE__,"MySQL::fetch() rs=[".$rs."]");
		if(mysql_num_rows($rs)){
			$ret=mysql_fetch_array($rs,MYSQL_ASSOC);
		}else{
			$ret=array();
		}
		return $ret;
	}

	public function decodeType($type,$size=""){
		switch($type){
			case "integer":
				return "BIGINT";
			case "text":
				if($size>1000){
					return "TEXT";
				}else if($size>4000){
					return "BIGTEXT";
				}else{
					return "TINYTEXT";
				}
				return "TEXT";
			case "date":
				return "DATETIME";
			case "char":
				return "CHAR($size)";
			case "string":
				return "VARCHAR($size)";
		}
	}
	/**
	 * (non-PHPdoc)
	 * @see modules/interfaces/IConnection#executeScript($file)
	 */
	public function executeScript($filesql){
		__debug(__FILE__,__LINE__,"MySQL::execute() sql=[".$filesql."]");
		$file=fopen($filesql,"r");
		while($line=fgets($file)){
			__debug(__FILE__,__LINE__,"line=$line");
			if(substr($line,1,2)!="--"){
				$this->execute($line);
			}
		}
		fclose($file);
	}
	/**
	 * return the static instance of the THeme Manager.
	 * create the instance if does not already exist.
	 */
	public function getInstance(){
		if(!isset(self::$_instance)){
			self::$_instance = new MySQL();
		}
		return self::$_instance;
	}
}
?>